package com.diyunapp.xilinapp.myindent;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import butterknife.OnClick;

// 查看物流
public class CheckLogisticsActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back1)
    ImageView goBack1;
    @Bind(R.id.express)
    TextView express;
    @Bind(R.id.express_company)
    RelativeLayout express_company;
    @Bind(R.id.wuliunull)
    TextView wuliunull;
    @Bind(R.id.express_num)
    TextView expressNum;
    @Bind(R.id.express_state)
    TextView expressState;
    @Bind(R.id.express_fresh_lv)
    ListView expressFreshLv;
    @Bind(R.id.express_refresh_view)
    SwipeRefreshLayout expressRefreshView;
    @BindColor(R.color.colorDarkTopBar)
    int pressColor;
    @BindColor(R.color.colorText0)
    int unpressColor;
    private List info = new ArrayList();
    private LogisticsAdapter adapter;
    private LayoutInflater inflater;
    private int[] bannerPoint;
    private static boolean isFirst = true;
private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_logistics);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.checklogistic_menubar_inc);
        super.MenuWidget(headView);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        initView();
        id=getIntent().getStringExtra("order_id");
        Log.i("zhangxiaodebug","wulidid="+id);
//        toast(id);
        adapter = new LogisticsAdapter(this);
        getData();
        expressFreshLv.setAdapter(adapter);

    }
    @OnClick(R.id.go_back1)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back1:
                finish();
                break;
        }
    }
    private void initView() {
        expressRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        expressRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                getData();
            }
        });}
    public void getData() {
//        String url = "" + MainActivity.url + "act=member_order&op=search_deliver&key=fbcdca5b1216edd0bec677aa41584452&order_id=46";
        String url = ""+MainActivity.url+"act=member_order&op=search_deliver&" +
                "key="+ MainActivity.myLogin.getLoginkey()+"&order_id="+id+"";

        Log.d("zhangxiaoxidebug", "my"+url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        expressRefreshView.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
//                                toast(String.valueOf(result));
                            if (result == 200) {
                                JSONObject information = resultObject.optJSONObject("datas");
                                String error=information.optString("error");
                                if(error!=null){
                                    wuliunull.setVisibility(View.VISIBLE);
                                    wuliunull.setText("当前暂无物流消息");
                                    express.setText("");
                                    expressNum.setText("");

                                }
                                String express_name = information.optString("express_name");
                                String shipping_code = information.optString("shipping_code");
                                express.setText(express_name);
                                expressNum.setText(shipping_code);
                                if(shipping_code!=""){
                                JSONArray listArray = information.optJSONArray("deliver_info");
                                for (int i = 0; i < listArray.length(); i++) {
                                    String delivers = listArray.optString(i);
                                    info.add(i, delivers);
                                    Log.i("zhangxiaoxidebug", "resultinfo=" + info);
                                }
                                adapter.setList(info);}
//                            }
//                                Log.i("zhangxiaoxidebug", "info=" + adapter);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        expressRefreshView.setRefreshing(false);
                        toast("请求失败");
                    }
                });
    }

    class LogisticsAdapter extends BaseAdapter {
        private TextView line1;
        private ImageView point;
        private  TextView line;
        private TextView tvAdress;
        private  TextView tvTime;
        private List info = new ArrayList();
        private Context context;

        public LogisticsAdapter(Context context) {
            this.context = context;
        }

        public void setList(List list) {
            this.info = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return null == info ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return null == info ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
//        private ViewHolder viewHolder;
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view;
//            if(convertView==null){

                view = LayoutInflater.from(context).inflate(R.layout.item_logistics, null);

//            }else{
//                view = convertView;
//            }
            line1 = (TextView) view.findViewById(R.id.line1);
            point = (ImageView) view.findViewById(R.id.point);

            line = (TextView) view.findViewById(R.id.line);
            tvAdress = (TextView) view.findViewById(R.id.tvAdress);
            tvTime = (TextView) view.findViewById(R.id.tvTime);
//            pressColor=view.findViewById(R.color.colorDarkTopBar)
//
            if (position == 0) {
                tvAdress.setTextColor(pressColor);
                tvTime.setTextColor(pressColor);
                point.setImageResource(R.drawable.picture_on);
                line1.setVisibility(View.INVISIBLE);
                line.setTextColor(pressColor);
            }
            String c= String.valueOf(info.get(info.size()-1));
            String d[]=c.split("&nbsp;&nbsp;");
            expressState.setText(d[1]);
            String a = String.valueOf(info.get(info.size() - position - 1));
            String b[] = a.split("&nbsp;&nbsp;");
            tvAdress.setText(b[1]);
            tvTime.setText(b[0]);
            return view;

        }
    }
}
